import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.datasets import load_breast_cancer

#加载数据
cancer=load_breast_cancer()
x=cancer.data
y=cancer.target
print('x:',x.shape)

#建立模型
model=PCA(2)
Z=model.fit_transform(x)
print('z:',Z.shape)

#常用参数
print('特征向量',model.components_)
print('特征值',model.explained_variance_)
print('特征值方差所占比例',model.explained_variance_ratio_)

plt.scatter(Z[:,0],Z[:,1],c=y)
plt.show()

#重建
x_=model.inverse_transform(Z)
print(x_.shape)

